package com.dk.testflow;

import com.dk.domain.ApiAutoTestEntity;
import com.dk.enumerate.DkRuleEnum;
import com.dk.factory.FactoryProducer;
import com.dk.factory.ReqFactory;
import com.dk.util.http.req.ReqContext;
import com.dk.util.http.res.ResUtil;
import com.dk.util.log.Log;
import okhttp3.Response;

/**
 * 接口请求环节
 */
public class ApiRequestLink extends ApiAutoTestChain{
    /**
     * 接口请求执行链
     * @param apiAutoTestEntity 接口自动化测试对象
     */
    @Override
    public void executeTestChain(ApiAutoTestEntity apiAutoTestEntity) {
        Log.info("---【执行用例】:用例编号:"+apiAutoTestEntity.getCaseNum()+";用例名称:"+apiAutoTestEntity.getCaseName()+"-开始发送请求");
        //获取请求工厂对象
        ReqFactory reqFactory = (ReqFactory) FactoryProducer.getFactory(DkRuleEnum.REQ.getValue());
        Response res = new ReqContext(reqFactory.getReq(apiAutoTestEntity.getMethod())).doRequest(apiAutoTestEntity);
        //获取用例编码
        String caseNum = apiAutoTestEntity.getCaseNum();
        //设置当前测试用例对象的响应头，响应编码，响应体
        apiAutoTestEntity.setResCode(ResUtil.getResCode(res,caseNum));
        apiAutoTestEntity.setResHeaders(ResUtil.getResHeaders(res,caseNum));
        apiAutoTestEntity.setResBody(ResUtil.getResBody(res,caseNum));
        //设置测试用例对象的响应对象信息
        //apiAutoTestEntity.setResponse(res);
        Log.info("---【执行用例】:用例编号:"+apiAutoTestEntity.getCaseNum()+";用例名称:"+apiAutoTestEntity.getCaseName()+"-获取响应成功");
//        Log.info("---【执行用例】:请求成功,响应码:"+ ResUtil.getResCode(res,caseNum)+
//                ";\n响应头:"+ResUtil.getResHeaders(res,caseNum)+
//                ";\n响应主体:"+ResUtil.getResBody(res,caseNum)+";");
    }
}
